package com.my.helper.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * 项目名称：Helper
 * 创建人：
 * 创建时间：2018/5/5 16:18
 */

public class MyDataBaseHelper extends SQLiteOpenHelper {
    private Context mContext;
    /*** 创建备忘录表 */
    public static final String CREATE_MEMORANDUM = "create table MyMemorandum (" +
            "id integer primary key autoincrement, " +
            "content text, " +
            "uid int," +
            "date text)";

    /*** 创建浏览历史表*/
    public static final String CREATE_HISTORY = "create table MyHistory (" +
            "id integer primary key autoincrement, " +
            "url text, " +
            "uid int," +
            "title text," +
            "type text," +
            "time text)";

    public MyDataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_MEMORANDUM);
        db.execSQL(CREATE_HISTORY);
//        Toast.makeText(mContext, "创建数据库成功", Toast.LENGTH_SHORT).show();
        Log.i("mwb", "数据库创建成功");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void addContent(int id, String content, String time) {
        ContentValues cv = new ContentValues();
        SQLiteDatabase db = this.getWritableDatabase();

        cv.put("content", content);
        cv.put("uid", id);
        cv.put("date", time);
        cv.clear();
        db.insert("MyMemorandum", null, cv);
        db.close();
        Log.i("mwb", "数据写入成功");
    }

    /**
     * 查询数据
     */
    public Cursor getContent(int id) {
        StringBuilder buf = new StringBuilder("SELECT * FROM MyMemorandum");
        SQLiteDatabase db = this.getWritableDatabase();
        return (db.rawQuery(buf.toString(), null));

    }

//    //我们希望创建一个名为Helper.db的数据库, 然后在数据库中新建一张BW表,
//    // 其中有id(主键), 内容, 时间 SQL建表语句如下:
//
//    /****
//     *  integer: 表示整型;
//     *  real: 表示浮点型;
//     *  text: 表示文本型;
//     *  blob:表示二进制类型
//     *  primary key: 表示主键;
//     *  autoincrement: 表示自增长
//     */
//    private static final String DATABASE_NAME = "Helper.db";//数据库名称
//    private static final int SCHEMA_VERSION = 1;
//
//    public MyDataBaseHelper(Context context) {
//        super(context, DATABASE_NAME, null, SCHEMA_VERSION);
//        mContext = context;
//    }
//
//    @Override
//    public void onCreate(SQLiteDatabase db) {
////        db.execSQL(CRATE_BW);
////        db.execSQL("CREATE TABLE IF NOT EXISTS dateContentList(" +
////                "id INT primary key autoincrement," +
////                "content TEXT ," +
////                "date LONG); "); // 创建了dateContentList的表 放着id,content,date
//        db.execSQL(CREATE_BOOK);
//        Toast.makeText(mContext, "创建数据库成功", Toast.LENGTH_SHORT).show();
//    }
//
//    @Override
//    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//
//    }
//
//    /**
//     * 向dateContentList表添加数据
//     *
//     * @param id
//     * @param content
//     * @param date
//     */
//    public void addDate(int id, String content, long date) {
//        ContentValues cv = new ContentValues();
//        SQLiteDatabase db = this.getWritableDatabase();
//        cv.put("id", id);
//        cv.put("content", content);
//        cv.put("date", date);
//        db.insert("dateContentList", null, cv);
//        db.close();
//    }
//
//    public Cursor getDate(String where, String orderBy) {
//        // 查询 备忘录信息
//        StringBuilder buf = new StringBuilder("SELECT * FROM dateContentList");
//        SQLiteDatabase db = this.getWritableDatabase();
//        if (where != null) {
//            buf.append(" WHERE ");
//            buf.append(where);
//        }
//
//        if (orderBy != null) {
//            buf.append(" ORDER BY ");
//            buf.append(orderBy);
//        }
//
//        return (db.rawQuery(buf.toString(), null));
//    }
//
//    public void deleteAlldateContentList() {
//        SQLiteDatabase db = this.getWritableDatabase();
//        db.execSQL("delete from dateContentList");
//        db.close();
//    }
//
//    public void cancelDataContentByTime(long date) {
//        SQLiteDatabase db = this.getWritableDatabase();
//        db.execSQL("delete from dateContentList where date= " + date);
//    }
//
//    public void updateDataContent(String content, long date) {
//        SQLiteDatabase db = this.getWritableDatabase();
//
//        db.execSQL("UPDATE dateContentList SET content = " + content + "'"
//                + " WHERE date = " + date);
//
////        db.execSQL("UPDATE dateContentList SET date ="+date);
//    }
}
